<template>
	<view class="content padding">
		<view class="padding ffffff">
			<view class="color_san text_x">
				上传发票图片
			</view>
			<view class="ffffff radius">
				<view class="flexleft flex_wrap">
					<view class="item_image_box margin_top" v-for="(item,index) in martial_images_arr" :key="index">
						<view class="item_image radius">
							<image :src="item" mode=""></image>
							<view class="deletimg" @click="deleimage(index,'martial_images')"></view>
						</view>
					</view>
					<!-- 图片盒子 -->
					<view class="item_image_box margin_top" @click="uploadImg('martial_images',1)"
						v-if="martial_images_arr.length<1">
						<view class="item_image radius">
							<image src="/static/image/home/noimage.png" mode=""></image>
							<!-- <view class="deletimg"></view> -->
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="footer padding ffffff">
			<view class="footerButton" @click="submit">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				order_id:"",//发票id
				martial_images: [], //资料图片
				martial_images_arr: [], //资料图片
			}
		},
		onLoad(options) {
			this.order_id=options.id
		},
		methods: {
			deleimage(index, value) {
				var that = this;
				if (value == "martial_images") {
					that.martial_images_arr.splice(index, 1)
					that.martial_images.splice(index, 1)
				}
			},
			uploadImg(value, length) {
				var that = this;
				var martial_images = that.martial_images
				var martial_images_arr = that.martial_images_arr
				if (martial_images.length >= length) {
					httpRequest.toast('最多上传' + length + '张图片');
					return;
				}
				this.upFile.chooseImage({
					count: length - martial_images.length
				}, function(res) {
					console.log(res);
					martial_images_arr.push(res.fullurl)
					martial_images.push(res.url)
					console.log(martial_images_arr, martial_images);
					that.martial_images_arr = martial_images_arr
					that.martial_images = martial_images
				})
			},
			submit(){ //提交
				let _this = this;
				httpRequest.request('/api/shop.goods_order/make_invoice', 'POST', {
					order_id: _this.order_id,
					image:_this.martial_images.join()
				}, false, false, true).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg);
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item_image_box {
		width: 33.33%;
	}

	.item_image {
		position: relative;
		width: 180rpx;
		height: 180rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>